package com.ruoyi.modules.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.modules.domain.AgentLevel;
import com.ruoyi.modules.service.IAgentLevelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 代理等级Controller
 *
 * @author ruoyi
 * @date 2022-02-03
 */
@Api(tags = "后台接口 - 代理等级")
@RestController
@RequestMapping("/backstage/agentLevel")
public class AgentLevelController extends BaseController {
    @Autowired
    private IAgentLevelService agentLevelService;

    /**
     * 查询代理等级列表
     */
    @PreAuthorize("@ss.hasPermi('modules:level:list')")
    @ApiOperation(value="代理等级列表", notes="代理等级列表")
    @GetMapping("/list")
    public TableDataInfo list(AgentLevel agentLevel) {
        startPage();
        List<AgentLevel> list = agentLevelService.list(new LambdaQueryWrapper<AgentLevel>().orderByAsc(AgentLevel::getDayNum));
        return getDataTable(list);
    }

    /**
     * 导出代理等级列表
     */
    @PreAuthorize("@ss.hasPermi('modules:level:export')")
    @Log(title = "代理等级", businessType = BusinessType.EXPORT)
    @ApiOperation(value="导出表格", notes="导出表格")
    @GetMapping("/export")
    public AjaxResult export(AgentLevel agentLevel) {
        List<AgentLevel> list = agentLevelService.list(new QueryWrapper<>(agentLevel));
        ExcelUtil<AgentLevel> util = new ExcelUtil<AgentLevel>(AgentLevel.class);
        return util.exportExcel(list, "代理等级数据");
    }

    /**
     * 获取代理等级详细信息
     */
    @PreAuthorize("@ss.hasPermi('modules:level:query')")
    @ApiOperation(value="代理等级详情", notes="代理等级列情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(agentLevelService.getById(id));
    }

    /**
     * 新增代理等级
     */
    @PreAuthorize("@ss.hasPermi('modules:level:add')")
    @Log(title = "代理等级", businessType = BusinessType.INSERT)
    @ApiOperation(value="代理等级新增", notes="代理等级新增")
    @PostMapping
    public AjaxResult add(@RequestBody AgentLevel agentLevel) {
        return toAjax(agentLevelService.save(agentLevel));
    }

    /**
     * 修改代理等级
     */
    @PreAuthorize("@ss.hasPermi('modules:level:edit')")
    @Log(title = "代理等级", businessType = BusinessType.UPDATE)
    @ApiOperation(value="代理等级修改", notes="代理等级修改")
    @PutMapping
    public AjaxResult edit(@RequestBody AgentLevel agentLevel) {
        return toAjax(agentLevelService.updateById(agentLevel));
    }

    /**
     * 删除代理等级
     */
    @PreAuthorize("@ss.hasPermi('modules:level:remove')")
    @Log(title = "代理等级", businessType = BusinessType.DELETE)
    @ApiOperation(value="代理等级删除", notes="代理等级删除")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(agentLevelService.removeByIds(Arrays.asList(ids)));
    }

}
